<?php

include('functions.php');
$string = isset($_POST['trans']) && $_POST['trans'] ? $_POST['trans'] : '';
$file_prefix = isset($_POST['file_prefix']) && $_POST['file_prefix'] ? $_POST['file_prefix'] : '';
$file_name = isset($_POST['file_name']) && $_POST['file_name'] ? $_POST['file_name'] : '';
$string = stripslashes($string);
// patter to match anything within '<' and '>'  also '{' and '}' tag later on we will split the string by <*> and {*} tag
$pattern = '/<[^<]*>|{[^\{]*\}/';

if($string != '' && $file_prefix != ''):
	
	// open files 
	$translated_file = fopen($file_name.'.html.txt','w+');
	$language_file = fopen($file_name.'.php.txt','w+');

	//we will parse line by line
	$file_lines = explode("\n",$string);
	
	//starting tag
	fwrite($language_file,"<?php\n\r");
	
	foreach($file_lines as $line)
	{
		
		$replace_bunch = preg_split($pattern,$line);
		$translated_line = $line;
	

	
		if(count($replace_bunch))
		{
			foreach($replace_bunch as $packet)
			{
				$packet = trim($packet);
				
				if($packet != '' && not_nbsp($packet) && not_special($packet) ):
					$short_name = get_short_name($packet);

					$replace_pattern = "/&nbsp;/";
					$packet = preg_replace($replace_pattern,"",$packet);					

					$lang_term = "\$msg['".$file_prefix."']['".$short_name."'] = \"$packet\";";
					fwrite($language_file,$lang_term . "\n\r");
					$translated_term = "{\$msg.".$file_prefix.".".$short_name."}";
					$translated_line = str_replace($packet,$translated_term,$translated_line);
				endif;
			}
		}
		fwrite($translated_file,$translated_line);
	
	
	}
	


endif;


?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Feak Translation !!!</title>
<style>
body
{
	font-family:Verdana, Arial, Helvetica, sans-serif;
	font-size:10pt;
	color:#000066;

}
#page
{
	width:930px;
	margin:10pt auto;

}
.button
{
	border:1px solid #666666;
	background:#CCCCCC;
	color:#990000;
	font-family:Geneva, Arial, Helvetica, sans-serif;
	font-weight:bold;
	
}
a
{
	color:#000099;
	text-decoration:none;
}
</style>
</head>

<body>
<div id ="page">
<div style="text-align:center;width:100%">Automated Sermon File Translation.</div><br /><br /><br />
Translated HTML file:

<a href="<?php print $file_name?>.html.txt" target="_blank" > here </a>
<br /><br />
Generated Language file:
<a href="<?php print $file_name?>.php.txt"  target="_blank"> here </a>
<br /><br />
<div style="text-align:center;width:100%"><a href='javascript:back(-1)'>Go Back</a></div><br /><br /><br />

</div>
</body>
</html>

